#ifndef PLAYER_H
#define PLAYER_H

#include <QtGui>
#include <QVariant>
#include <math.h>

#include "mainwindow.h"
#include "shipobject.h"
#include "shot.h"
#include "gameworld.h"

using namespace std;

class Player : public QObject
{
    Q_OBJECT
public:
    Player(QString initName) : name (initName){}

    explicit Player(QObject *parent = 0);
    bool placeShip(QString type, QString whichPlayer);
    bool addShipIfPossible(ShipObject &ship, QString whichPlayer);

    enum {
        EASYDIM = 8,
        NORMALDIM = 10,
        HARDDIM = 20,
        SHIPS = 5,
        SHIPS2 = 1,
        SHIPS3 = 2,
        SHIPS4 = 1,
        SHIPS5 = 1
    };



private:
    QString name;
    int score;

    //hold different shot types to grid cells.
    enum RESULT {
        NOT_SHOT = 0,
        SHOT_ON_WAY = 1,
        HIT = 2,
        MISS = 3
    };

    //differentiate shooting directions
    enum DIRECTIONS {
        LEFT, RIGHT, UP, DOWN
    };

    // tell if last shot sunk enemy ship
    // tell the playerHit slot that the shot
    // will not be placed into the m_HitShots list
    bool m_LastShotSunkShip;

    //our shots
    RESULT m_Shots[EASYDIM][EASYDIM];

    // lits of shots that are hits to ships that are still floating
    QList<Shot> m_HitShots;

    int m_OpponentShips[4];
    void resetState();
    void removeShotFromList(const Shot & shot);
    void shuffleList(QList<DIRECTIONS> &list);
    bool hasBeenShot(const Shot &shot, RESULT result) const;
    void shoot(const Shot &shot);

    //for AI
    bool tryToShootRight(const Shot &shot, bool iterateHits = false);
    bool tryToShootDown(const Shot &shot, bool iterateHits = false);
    bool tryToShootLeft(const Shot &shot, bool iterateHits = false);
    bool tryToShootUp(const Shot &shot, bool iterateHits = false);
    bool canContainShip(const Shot &shot) const;


signals:
    void log(QVariant logText);
    void myShipsPlaced();
    void shootGuns(QVariant row, QVariant column);
    void gotHit(QVariant gotHit, QVariant row, QVariant column);
    void sinks(QVariant hp, QVariant row, QVariant column, QVariant portrait);


public slots:
    void shipsPlaced();
    void gunFired(int row, int column);
    void playerHit(bool youHit, int row, int column);
    void sunk(int hp, int row, int column, bool portrait);

    void decideWhereToShoot();

};

#endif // PLAYER_H
